package com.treasure.mall.biz.entity.embeddable;

import com.treasure.mall.biz.entity.product.ProductPO;
import com.treasure.mall.zbq.common.ienum.base.CodeDescEnumHelper;
import com.treasure.mall.zbq.common.ienum.product.ProductEnums;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.math.BigDecimal;

/**
 * @author allan
 * @date 2019-10-21
 */
@Embeddable
@Setter
@Getter
@NoArgsConstructor
public class GoodsInfo {
    @Column(name = "goods_id")
    private Long goodsId;
    @Column(name = "goods_bn")
    private String goodsBn;
    @Column(name = "goods_name")
    private String goodsName;
    @Column(name = "category_id")
    private Long categoryId;
    @Column(name = "cate_name")
    private String cateName;
    /**
     * 零售价格
     */
    @Column(name = "price", scale = 2, precision = 10)
    private BigDecimal price;

    /**
     * 商品缩略图
     */
    @Column(name = "thumbnail")
    private String thumbnail;
    /**
     * 平台服务费比例
     */
    @Column(name = "service_rate", scale = 3, precision = 6)
    private BigDecimal serviceRate;
    /**
     * 百分比
     */
    @Column(name = "rebate_rate_one", scale = 3, precision = 6)
    private BigDecimal rebateRateOne;
    /**
     * 百分比
     */
    @Column(name = "rebate_rate_two", scale = 3, precision = 6)
    private BigDecimal rebateRateTwo;
    @Column(name = "product_type")
    private ProductEnums.ProductType productType;
    /**
     * 支持7天无理由退换货
     */
    @Column(name = "enable7DaysReturnedPurchase")
    private Boolean enable7DaysReturnedPurchase;
    /**
     * 全国包邮
     */
    @Column(name = "enableNationwidePostingFree")
    private Boolean enableNationwidePostingFree;
    /**
     * 属于哪个直播间
     */
    @Column(name = "room_id")
    private Long roomId;

    public GoodsInfo(ProductPO productPO) {
        this.goodsId = productPO.getProductId();
        this.goodsName = productPO.getProductName();
        this.price = productPO.getPrice();
        this.thumbnail = productPO.getMainPic();
        this.roomId = productPO.getRoomId();
        this.productType = CodeDescEnumHelper.getEnumTypeByCode(ProductEnums.ProductType.class, productPO.getProductType());
        this.serviceRate = productPO.getCommissionRate();
        if (productPO.getCategory() == null) {
            this.categoryId = productPO.getMerchantPO().getData().getMainCategory().getCatId();
            this.cateName = productPO.getMerchantPO().getData().getMainCategory().getCatName();
        } else {
            this.categoryId = productPO.getCategory().getCatId();
            this.cateName = productPO.getCategory().getCatName();
        }
        // TODO: 2020/6/18 如果商品上没有，则取类目的值
//        this.rebateRateOne = productPO.getCategory().getRebateRateOne();
//        this.rebateRateTwo = productPO.getCategory().getRebateRateTwo();
        this.enable7DaysReturnedPurchase = productPO.getServiceTags().contains(ProductEnums.ServiceTag.SEVEN_DAYS_NO_REASON_TO_REFUND.getCode().toString());
        this.enableNationwidePostingFree = productPO.getServiceTags().contains(ProductEnums.ServiceTag.FREE_SHIPPING_NATIONALLY.getCode().toString());
    }
}
